﻿using System;
using System.Web;
using System.Drawing;
using ManagedFusion.Web.Controls;

public class CaptchaManager
{
    public static string GetCaptchaString()
    {
        string result;
        //we use current request so that all the captchas for a single will
        //be the same (meaning sendToFriend and add reply...)
        if (HttpContext.Current.Session["captchaText"]!=null)
        {
            result = HttpContext.Current.Session["captchaText"] as string;
        }
        else
        {
            result = Guid.NewGuid().ToString("N").Substring(0, CaptchaImage.TextLength);            
            HttpContext.Current.Session["captchaText"] = result;
        }
        return result;
    }

    public static void ResetCaptcha()
    {
        HttpContext.Current.Session["captchaText"] = null;
    }

    public static Bitmap RenderImage()
    {
        CaptchaImage ci = new CaptchaImage()
        {
            Text = GetCaptchaString()
        };
        return ci.RenderImage();
    }

    public static bool IsValid
    {
        get
        {
            string request = HttpContext.Current.Request["captchaText"];
            string user = HttpContext.Current.Session["captchaText"] as string;
            return request == user;
        }
    }
}